//前台跳转路由
const axios = require('axios');
const express = require('express')
const fs = require('fs')
const template = require('art-template')
const jump = express.Router()
const http = require('http')
//主页功能
let index = require('./index')
jump.use('/index', index)
//首页
jump.get('/picture/homePage', (req, res) => {
    axios.get('/home/query')
    .then(data => {
        let arr = data.data.data
        res.render('../views/index.art', {
            session: req.session,
            arr
        })
    })
    .catch(err => {
        console.log(err)
    })
})

//分页2
jump.get('/picture/pages', (req, res) => {
    res.render('../views/common/pages.art')
})

//图片搜素
jump.get('/picture/search', (req, res) => {
    res.render('../views/search.art', {
        session: req.session
    })
})
//用户搜索
jump.get('/picture/userSearch', (req, res) => {
    res.render('../views/userSearch.art', {
        session: req.session
    })
})
//专辑搜索
jump.get('/picture/albumSearch', (req, res) => {
    res.render('../views/albumSearch.art', {
        session: req.session
    })
})
//分页
jump.get('/picture/page', (req, res) => {
    res.render('../views/page.html')
})
//分类的页面
jump.get('/picture/classfy', (req, res) => {
    let {url} = req
    let fatherId = url.split('=')[2]
    axios.get(`/home/subclassificationList?fatherId=${fatherId}`)
    .then(data=>{
        res.render('../views/classfy.art', {
            session: req.session,list:data.data.data
        }) 
    })
    .catch(err=>{
        res.status(500).json({
            err:err
        })
    })
})
//写文章的页面////要把它放到个人主页
jump.get('/picture/writeArtical', (req, res) => {
    res.render('../views/writeArtical.art')
})
//发布文章
jump.get('/picture/publish', (req, res) => {
        if(req.session.userMessage!=undefined){
            let userId = req.session.userMessage.id
            async function getall(userId) {
                let obj = {}
                let albumAns = await axios.get(`/user/getAlbumList?userId=${userId}`,{headers:{token:(req.session.token?req.session.token:'')}})
                album = albumAns.data.data
                obj['album'] = album
                let classAns = await axios.get(`/home/getSmall`,{headers:{token:(req.session.token?req.session.token:'')}})
                classfy = classAns.data.data
                obj['classfy'] = classfy
                return obj
            }
            getall(userId).then(data => {
                res.render('../views/common/publish.art', { data })
            })
        }else{
            res.render('../views/common/login.art')
        }
        // let userId = 1
        //     async function getall(userId) {
        //         let obj = {}
        //         let albumAns = await axios.get(`/user/getAlbumList?userId=${userId}`)
        //         album = albumAns.data.data
        //         obj['album'] = album
        //         let classAns = await axios.get(`/home/getSmall`)
        //         classfy = classAns.data.data
        //         obj['classfy'] = classfy
        //         return obj
        //     }
        //     getall(userId).then(data => {
        //         res.render('../views/common/publish.art', { data })
        //     })
    })
    // //点击文章看图片详情
    // if(req.session.userMessage!=undefined){
    //     let userId = req.session.userMessage.id
    //     async function getall(userId) {
    //         let obj = {}
    //         let albumAns = await axios.get(`/user/getAlbumList?userId=${userId}`)
    //         album = albumAns.data.data
    //         obj['album'] = album
    //         let classAns = await axios.get(`/home/getSmall`)
    //         classfy = classAns.data.data
    //         obj['classfy'] = classfy
    //         return obj
    //     }
    //     getall(userId).then(data => {
    //         res.render('../views/common/publish.art', { data })
    //     })
    // }else{
    //     res.render('../views/common/login.art')
    // }
//点击文章看图片详情
jump.get('/picture/picDetail', (req, res) => {
        let sessions=req.session
        let postId = req.url
        postId=postId.split('=')[1]
        if(sessions.userMessage){
            axios.get(`/index/searchForumById?postId=${postId}&userId=${sessions.userMessage.id}`,{headers:{token:(req.session.token?req.session.token:'')}})
            .then(data => {
                let info = data.data.data
                res.render('../views/picDetail.art', { info, session: sessions })
            })
        }else{
            axios.get(`/index/searchForumById?postId=${postId}`)
            .then(data => {
                let info = data.data.data
                res.render('../views/picDetail.art', { info , session:'' })
            })
        }

    })
//     //测试发布文章的弹窗
// jump.get('/picture/publish', (req, res) => {
//     res.render('../views/common/publish.art')
// })


//个人设置页面
jump.get(`/picture/set`, (req, res) => {
    console.log(111)
    if(req.session.userMessage==undefined){
        res.render('../views/common/login.art')
    }else{
        let id = req.session.userMessage.id
        axios.get(`/user/getUserInfo?userId=${id}`,{headers:{token:(req.session.token?req.session.token:'')}})
            .then(data => {
                let info = data.data.data
                res.render('../views/set.art', { info, session: req.session })
            })
    }
})


jump.get('/register', (req, res) => {
    res.render('../views/common/register.art', {
        session: req.session
    })
})

//登录
jump.get('/login', (req, res) => {
    res.render('../views/common/login.art')
})
//导航条
jump.get('/headNav', (req, res) => {
    res.render('../views/common/headNav.art')
})
//底部
jump.get('/bottomNav', (req, res) => {
    res.render('../views/common/bottomNav.art')
})
//个人主页专辑
jump.get('/picture/personmainpage', (req, res) => {
    // if(req.url.split('=')[1]==undefined&&req.session.userMessage==undefined){
            // res.render('../views/common/login.art')
    // }else{
        res.render('../views/personmainpage.art', {
            session: req.session
        })
    // }
})
//个人主页中的专辑收藏页面
jump.get('/album/collection', (req, res) => {
    if(req.url.split('=')[1]==undefined&&req.session.userMessage==undefined){
        res.render('../views/common/login.art')        
    }else{
        res.render('../views/albumcollect.art', {
            session: req.session
        })
    }
})
//个人主页中的文章收藏页面
jump.get('/passage/collection', (req, res) => {
    res.render('../views/passagecollect.art', {
        session: req.session
    })
})
//个人主页中的图片页面
jump.get('/picture/picture', (req, res) => {
    // if(req.url.split('=')[1]==undefined&&req.session.userMessage==undefined){
    //     res.render('../views/common/login.art')
    // }else{
        res.render('../views/picture.art', {
            session: req.session
        })
    // }
})
    //个人主页中的图片页面,阅读一篇文章
jump.get('/picture/introducearticle', (req, res) => {
        res.render('../views/introducearticle.art', {
            session: req.session
        })
    })

  
    //主页
jump.get('/picture/homePage', (req, res) => {
        res.render('../views/index.art', {
            session: req.session
        })
    })
    //个人主页-文章收藏-阅读一篇文章
jump.get('/read/passage', (req, res) => {
    // if(req.session.userMessage){
        res.render('../views/readpassage.art', { session: req.session })
    // }else{
        res.render('../views/common/login.art')
    // }
})

jump.get('/sort', (req, res) => {
    res.render('../views/common/waterfall.art')
})


jump.get('/sortspage', (req, res) => {
    res.render('../views/common/sortspage.art')

})

jump.get('/lianxi', (req, res) => {
    res.render('../views/common/lianxi.art')

})
jump.get('/index', (req, res) => {
        res.render('../views/index.art')


    })
    //测试
jump.get('/picture/test', (req, res) => {
    res.render('../views/test.art')
})

//验证码
jump.get('/examine', (req, res) => {
    res.render('../views/common/examine.art')
})

//我的粉丝
jump.get('/picture/fans', (req, res) => {
    if(req.session.userMessage){
        res.render('../views/common/fan.art', {
            session: req.session
        })
    }else{
        res.render('../views/common/login.art')
    }
})

//我的关注
jump.get('/picture/attantion', (req, res) => {
    if(req.session.userMessage){
        res.render('../views/attantion.art', {
            session: req.session
        })
    }else{
        res.render('../views/common/login.art')
    }
})

//404页面
jump.get('/404yemian', (req, res) => {
    res.render('../views/404.art', {
        session: req.session
    })
})
// //阅读一篇帖子页面
jump.get('/album/tiezi', (req, res) => {
    res.render('../views/tiezi.art', {
        session: req.session
    }
    )})


// //阅读一篇专辑页面
jump.get('/album/read', (req, res) => {
    res.render('../views/readalbum.art', {
            session: req.session,
        })
        
        // let { url } = req
        // console.log(url)
        // let albumId = url.split('=')[0]
        // axios({
        //     method: 'get',
        //     url: '/album/getAlbumList',
        //     params: {
        //         albumId: albumId
        //     }
        // }).then(data => {
        //     res.render('./views/readalbum.art', {
        //         session: req.session,
        //         albumId: albumId,
        //     })

    // }).catch(err => {
    //     console.log(err);
    // })


    // res.render('../views/readalbum.art', {
    //     session: req.session
    // })
    // })
    // jump.get('/album/read', (req, res) => {
    //         session: req.session

    //             res.render('../views/readalbum.art')
    //             // let { url } = req
    //             // let albumId = url.split('=')[1]
    //             // axios({
    //             //     method: 'get',
    //             //     url: '/album/getAlbumList',
    //             //     params: {
    //             //         albumId: albumId
    //             //     }
    //             // }).then(data => {
    //             //     res.render('readalbum.art', {
    //             //         albumId: albumId
    //             //     })

    //         // }).catch(err => {
    //         //     console.log(err);
    //         // })


    //     })
    //测试
    // var server=http.createServer();
    // // 设置响应
    // server.on('request',function(req,res){
    // 	// 读取文件
    // 	fs.readFile('../views/personmainpage.art',function(err,data){
    // 		if(err){
    // 			return console.log('404 Not Found.')
    // 		}
    // 		// 默认的data是二进制，我们要把data转成字符串，toString()
    // 		var ret=template.render(data.toString(),{
    // 			title:'个人信息',
    // 			name:'张三',
    // 			sex:'男',
    // 			age:'18'
    // 		})
    // 		// 设置浏览器识别类型和编码
    // 		res.setHeader('Content-Type','text/html;charset=utf-8')
    // 		console.log(ret)
    // 		// 响应到页面，结束。
    // 		res.end(ret)
    // 	})
    // })
})


module.exports = jump;